package com.tencent.cloud.uikit.ui.widget;

import android.content.Context;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.view.ViewGroup;
import android.widget.FrameLayout;

import com.tencent.cloud.uikit.utils.SystemUiUtils;

/**
 * 自适配状态栏、标题栏和导航栏高度的布局
 *
 * 使用此布局作为 Activity 根布局，内容区域将不会延伸至状态栏、标题栏和导航栏
 *
 * @author showxiao
 * @date 2018/8/22
 */
public class FitsWindowsFrameLayout extends FrameLayout {

    private static final String TAG = "FitsWindowsFrameLayout";

    private boolean isActionBarOverlay = false;
    private boolean isStatusBarOverlay = false;

    public FitsWindowsFrameLayout(Context context) {
        super(context);
        init(context);
    }

    public FitsWindowsFrameLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context);
    }

    public FitsWindowsFrameLayout(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context);
    }

    private void init(Context context) {
        setFitsSystemWindows(true);
        setClipToPadding(false);
        setLayoutParams(new ViewGroup.LayoutParams(
                ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT
        ));
    }

    @Override
    protected boolean fitSystemWindows(Rect insets) {
        if (!isActionBarOverlay && !isStatusBarOverlay) {
            return super.fitSystemWindows(insets);
        }

        Rect rect = new Rect(insets);
        if (isActionBarOverlay) {
            rect.top -= SystemUiUtils.getActionBarHeight(getContext());
        }
        if (isStatusBarOverlay) {
            rect.top -= SystemUiUtils.getStatusBarHeight(getContext());
        }

        return super.fitSystemWindows(rect);
    }

    public void setActionBarOverlay(boolean isActionBarOverlay) {
        this.isActionBarOverlay = isActionBarOverlay;
    }

    public void setStatusBarOverlay(boolean isStatusBarOverlay) {
        this.isStatusBarOverlay = isStatusBarOverlay;
    }
}
